﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace DAL
{
    public class StatusCodeService
    {
        public static StatusCode AddStatusCode(StatusCode sCode)
        {
            string sql = string.Format("insert into StatusCode(Id,Name) values({0},N'{1}');", sCode.Id, sCode.Name);
            DBHelper.GetScalar(sql);
            return GetStatusCodeById(sCode.Id);
        }

        public static StatusCode GetStatusCodeById(int id)
        {
            string sql = string.Format("select * from StatusCode where Id={0}", id);
            return GetStatusCodeBySql(sql);
        }

        public static List<StatusCode> GetAllStatusCodes()
        {
            string sql = string.Format("select * from StatusCode");
            return GetStatusCodesBySql(sql);
        }

        private static StatusCode GetStatusCodeBySql(string sql)
        {
            List<StatusCode> list = GetStatusCodesBySql(sql);
            if (list.Count() > 0)
                return list[0];
            return null;
        }

        private static List<StatusCode> GetStatusCodesBySql(string sql)
        {
            List<StatusCode> list = new List<StatusCode>();
            DataTable dt = DBHelper.GetDataSet(sql);
            foreach (DataRow row in dt.Rows)
            {
                StatusCode sCode = new StatusCode();
                sCode.Id = (int)row["Id"];
                sCode.Name = (string)row["Name"];
                list.Add(sCode);
            }
            return list;
        }
    }
}
